<?php
/**
 * cms首页
 *
 *
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');
class indexControl extends mobileHomeControl{

    public function __construct() {
        parent::__construct();
    }

    public function indexOp(){
        header("Content-type: text/html; charset=utf-8");
        $model_mb_ad = Model('adv');
        $datas = array();
        //获取活动内容
        $huodong_list = array();
        $num= 5;
        $start = 0;
        $limit = " limit ".$start.",".$num;
        $where  = " where 1=1 and a.ac_id in(17) ";
        $sql = "select a.article_id,a.ac_id,a.article_url,a.article_title,a.article_description,a.article_content,b.file_name from `".DBPRE."article` as a left join `".DBPRE."upload` as b on b.item_id = a.article_id ".$where." order by article_id desc ".$limit;

        $huodong_list = Db::getALl($sql);

        //获取加盟列表内容
        $jiameng_list = array();
        $num= 5;
        $start = 0;
        $limit = " limit ".$start.",".$num;
        $sql = "select a.*,b.file_name from `".DBPRE."franchiser` as a left join `".DBPRE."upload` as b on b.item_id = a.article_id order by areaid_1 asc ".$limit;
        $jiameng_list = Db::getALl($sql);


        //获取文章
        $article_list = array();
        // 获取首页文章列表
        $num= 5;
        $start = 0;
        $limit = " limit ".$start.",".$num;
        $where  = " where 1=1 and a.ac_id in(13,14) ";
        $sql = "select a.article_id,a.ac_id,a.article_url,a.article_title,a.article_description,a.article_content,b.file_name from `".DBPRE."article` as a left join `".DBPRE."upload` as b on b.item_id = a.article_id ".$where." order by article_id desc ".$limit;

        $article_list = Db::getAll($sql);

        $datas['article_list']=$this->article_list_extend($article_list);
        $datas['huodong_list'] = $this->article_list_extend($huodong_list);
        $datas['jiameng_list'] = $this->article_list_extend($jiameng_list);
        //获取首页文章结束
        //广告
        $adv_list = array();
        $advSql = "select * from `".DBPRE."adv` where ap_id in(374,375) and is_allow = 1 ";
        $adv_list = Db::getAll($advSql);
        if(count($adv_list))
        {
            for($i=0;$i<count($adv_list);$i++)
            {
                $adv_list[$i]['adv_content'] = unserialize($adv_list[$i]['adv_content']);

            }
        }
        $indexAdv=Model('web_special')->getOne(array('special_id'=>2));
        $datas['advSpecial']=html_entity_decode($indexAdv['special_content']);


        $datas['adv_list']= $adv_list;
        output_data($datas);
    }

    public function article_list_extend($article_list)
    {
        //header("Content-type: text/html; charset=utf-8");
        foreach($article_list as $key=>$value)
        {

            $article_list[$key]['article_content'] = preg_replace('/&nbsp;|&ldquo;|&rdquo;|\/r\/n/i','',trim(strip_tags($article_list[$key]['article_content'])));
            $article_list[$key]['file_name'] = "/data/upload/shop/article/".$article_list[$key]['file_name'];
        }

        return $article_list;
    }

    /**
     * 获得推荐商品
     */
    public function get_booth_goods($gc_id=1074) {
        $isMobile=true;
        if ($gc_id <= 0) {
            return false;
        }
        // 获取分类id及其所有子集分类id
        $goods_class = H('goods_class') ? H('goods_class') : H('goods_class', true);
        if (empty($goods_class[$gc_id])) {
            return false;
        }
        $child = (!empty($goods_class[$gc_id]['child'])) ? explode(',', $goods_class[$gc_id]['child']) : array();
        $childchild = (!empty($goods_class[$gc_id]['childchild'])) ? explode(',', $goods_class[$gc_id]['childchild']) : array();
        $gcid_array = array_merge(array($gc_id), $child, $childchild);
        // 查询添加到推荐展位中的商品id
        $boothgoods_list = Model('p_booth')->getBoothGoodsList(array('gc_id' => array('in', $gcid_array)), 'goods_id', 0, $isMobile?20:4, 'rand()');
        if (empty($boothgoods_list)) {
            return false;
        }

        $goodsid_array = array();
        foreach ($boothgoods_list as $val) {
            $goodsid_array[] = $val['goods_id'];
        }

        $fieldstr = "goods_id,goods_commonid,goods_name,goods_jingle,store_id,store_name,goods_price,goods_marketprice,goods_storage,goods_image,goods_freight,goods_salenum,color_id,evaluation_count";
        $goods_list = Model('goods')->getGoodsOnlineList(array('goods_id' => array('in', $goodsid_array)), $fieldstr);
        if (empty($goods_list)) {
            return false;
        }
        $commonid_array = array();
        foreach ($goods_list as $val) {
            $commonid_array[] = $val['goods_commonid'];
        }
        $groupbuy_list = Model('groupbuy')->getGroupbuyListByGoodsCommonIDString(implode(',', $commonid_array));
        $xianshi_list = Model('p_xianshi_goods')->getXianshiGoodsListByGoodsString(implode(',', $goodsid_array));
        foreach ($goods_list as $key => $value) {
            // 团购
            if (isset($groupbuy_list[$value['goods_commonid']])) {
                $goods_list[$key]['goods_price'] = $groupbuy_list[$value['goods_commonid']]['groupbuy_price'];
                $goods_list[$key]['group_flag'] = true;
            }
            if (isset($xianshi_list[$value['goods_id']]) && !$goods_list[$key]['group_flag']) {
                $goods_list[$key]['goods_price'] = $xianshi_list[$value['goods_id']]['xianshi_price'];
                $goods_list[$key]['xianshi_flag'] = true;
            }
        }
        return compact('goods_list','groupbuy_list','xianshi_list');

    }
}